package au.com.iglootech.component;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import com.itextpdf.text.Document;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import com.vaadin.terminal.StreamResource.StreamSource;

public class IGTPdf implements StreamSource {
    private final ByteArrayOutputStream os = new ByteArrayOutputStream();

    public IGTPdf() {
        Document document = null;

        try {
            document = new Document(PageSize.A4, 50, 50, 50, 50);
            PdfWriter.getInstance(document, os);
            document.open();

            document.add(new Paragraph("This is some content for the sample PDF!"));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (document != null) {
                document.close();
            }
        }
    }

    @Override
    public InputStream getStream() {
        // Here we return the pdf contents as a byte-array
        return new ByteArrayInputStream(os.toByteArray());
    }
}